﻿/*  ToolsMK - MathMK
 *  
 *  Copyright (C) 2009  Maik Kellerhals
 *  
 * This file is part of ToolsMK.
 *  
 *  ToolsMK is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */

/* FUNCTIONS
 * =========
 * 
 * deg2rad              - convert degrees to radians
 * rad2deg              - convert radians to degrees
 * random               - simplified random number generator
 * getAngle             - get angle betwenn 2 points
 * getPointFromAngle    - get a point from angle and distance
 * getDistance          - get a distance between 2 points
 * 
 */
package mk.mathMK {
	
	import flash.geom.Point;
	
	public class MathMK {
		
		public static function deg2rad(deg:Number):Number {
			return deg * Math.PI / 180;
		}
		
		public static function rad2deg(rad:Number):Number {
			return rad * 180 / Math.PI;
		}
		
		public static function random(from:Number, to:Number, round:Boolean=false):Number {
			if (round == true) {
				return Math.round(Math.random() * ((to+.5) - (from-.5)) + (from-.5));
			} else {
				return Math.random() * (to - from) + from;
			}
		}
		
		public static function getAngle(fromX:Number, fromY:Number, toX:Number, toY:Number):Number {
			return rad2deg(Math.atan2(toY - fromY, toX - fromX))
		}
		
		public static function getPointFromAngle(angle:Number, dist:Number):Point {
			var point:Point = new Point();
			var angleRad:Number = deg2rad(angle);
			point.x = dist * Math.sin(-angleRad);
			point.y = dist * Math.cos(angleRad);
			return point;
		}
		
		public static function getDistance(fromX:Number, fromY:Number, toX:Number, toY:Number, round:Boolean=false):Number {
			var dX:Number = toX - fromX;
			var dY:Number = toY - fromY;
			if (round == true) {
				return Math.round(Math.sqrt(dX*dX + dY*dY));
			} else {
				return Math.sqrt(dX*dX + dY*dY);
			}
		}
	}
}